package com.i8office.android.zhihuixinwu.module.model;

import com.cyan.tools.callback.RequestCallback;
import com.cyan.tools.util.SimpleSubscriber;
import com.i8office.android.zhihuixinwu.app.App;
import com.i8office.android.zhihuixinwu.bean.Code;
import com.i8office.android.zhihuixinwu.module.contract.AddOpinionContract;
import com.i8office.android.zhihuixinwu.util.OfficeSubscriber;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by pc on 2018/7/8.
 */

public class AddOpinionModel implements AddOpinionContract.Model {
    @Override
    public Disposable addOpinion(final RequestCallback<Code> callback, String content) {
        return App.getComponent().getAPIService().addPrivateOpinion(content).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new OfficeSubscriber<Code>(callback) {
            @Override
            public void onNext(Code o) {
                if (o.isSucceed())
                    callback.requestSuccess(o);
                else callback.requestError(o.getMessage());
            }
        });
    }

    @Override
    public Disposable saveCell(final RequestCallback<Code> callback, Map<String, String> params) {
        return App.getComponent().getAPIService().saveCell(params)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new OfficeSubscriber<Code>(callback) {
            @Override
            public void onNext(Code o) {
                if (o.isSucceed())
                    callback.requestSuccess(o);
                else callback.requestError(o.getMessage());
            }
        });
    }

    @Override
    public Disposable saveCells(final RequestCallback<Code> callback, List<Map<String, String>> paramsList) {
        final Observable[] sources = new Observable[paramsList.size()];
        for (int i = 0; i < paramsList.size(); i++) {
            sources[i] = App.getComponent().getAPIService().saveCell(paramsList.get(i));

        }
        final int[] num = {0};
        return (Disposable) Observable.concatArray(sources)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new OfficeSubscriber<Code>(callback) {
                    @Override
                    public void onNext(Code o) {
                        if (o.isSucceed()) {
                            num[0] += 1;
                            if (num[0] == sources.length) {
                                callback.requestSuccess(o);
                            }
                        } else {
                            callback.requestError(o.getMessage());
                        }

                    }
                });
    }
}
